####
library(exteriorMatch)
library(plyr)
source("/Users/sharpe/Desktop/Young Surgeons/taper_code/exterior_match_function_paired.R")

#######################################
#### Read in the matched datasets. ####

mod_match_ortho_0 <- read.csv("/Users/sharpe/Desktop/Young Surgeons/taper_data/lv_0_ortho_matched_mod_pats.csv")
mod_match_ortho_0$pair_id_pats <- paste(mod_match_ortho_0$pair_id_pats, "o", 0, sep="_")
mod_match_gensurg_0 <- read.csv("/Users/sharpe/Desktop/Young Surgeons/taper_data/lv_0_gensurg_matched_mod_pats.csv")
mod_match_gensurg_0$pair_id_pats <- paste(mod_match_gensurg_0$pair_id_pats, "g", 0, sep="_")
both_0 <- rbind.fill(mod_match_ortho_0, mod_match_gensurg_0)

mod_match_ortho_1 <- read.csv("/Users/sharpe/Desktop/Young Surgeons/taper_data/lv_1_ortho_matched_mod_pats.csv")
mod_match_ortho_1$pair_id_pats <- paste(mod_match_ortho_1$pair_id_pats, "o", 1, sep="_")
mod_match_gensurg_1 <- read.csv("/Users/sharpe/Desktop/Young Surgeons/taper_data/lv_1_gensurg_matched_mod_pats.csv")
mod_match_gensurg_1$pair_id_pats <- paste(mod_match_gensurg_1$pair_id_pats, "g", 1, sep="_")
both_1 <- rbind.fill(mod_match_ortho_1, mod_match_gensurg_1)

mod_match_ortho_2 <- read.csv("/Users/sharpe/Desktop/Young Surgeons/taper_data/lv_2_ortho_matched_mod_pats_12.21.2018.csv")
mod_match_ortho_2$pair_id_pats <- paste(mod_match_ortho_2$pair_id_pats, "o", 2, sep="_")
mod_match_gensurg_2 <- read.csv("/Users/sharpe/Desktop/Young Surgeons/taper_data/lv_2_gensurg_matched_mod_pats_1.2.2019.csv")
mod_match_gensurg_2$pair_id_pats <- paste(mod_match_gensurg_2$pair_id_pats, "g", 2, sep="_")
both_2 <- rbind.fill(mod_match_ortho_2, mod_match_gensurg_2)

mod_match_ortho_3 <- read.csv("/Users/sharpe/Desktop/Young Surgeons/data/simul_mod_patient_match_ortho_11.8.2018_exact_procs.csv")
mod_match_ortho_3$pair_id_pats <- paste(mod_match_ortho_3$pair_id_pats, "o", 3, sep="_")
mod_match_gensurg_3 <- read.csv("/Users/sharpe/Desktop/Young Surgeons/data/simul_mod_patient_match_gensurg_11.2.2018.csv")
mod_match_gensurg_3$pair_id_pats <- paste(mod_match_gensurg_3$pair_id_pats, "g", 3, sep="_")
both_3 <- rbind.fill(mod_match_ortho_3, mod_match_gensurg_3)

###############################################
#### Read in the updated icu_yes variable. ####

both_0 <- both_0[,-which(names(both_0) %in% c("total_cost_complete", "total_cost_index_only","icu_days","total_cost_other", "icu_yes"))]
both_1 <- both_1[,-which(names(both_1) %in% c("total_cost_complete", "total_cost_index_only","icu_days","total_cost_other", "icu_yes"))]
both_2 <- both_2[,-which(names(both_2) %in% c("total_cost_complete", "total_cost_index_only","icu_days","total_cost_other", "icu_yes"))]
both_3 <- both_3[,-which(names(both_3) %in% c("total_cost_complete", "total_cost_index_only","icu_days","total_cost_other", "icu_yes"))]


trad_ortho_icu <- read.csv("/Users/sharpe/Desktop/Young Surgeons/data/trad_ortho_new_cost_and_icu.csv")
mod_ortho_icu <- read.csv("/Users/sharpe/Desktop/Young Surgeons/data/mod_ortho_new_cost_and_icu.csv")
trad_gensurg_icu <- read.csv("/Users/sharpe/Desktop/Young Surgeons/data/trad_gensurg_new_cost_and_icu.csv")
mod_gensurg_icu <- read.csv("/Users/sharpe/Desktop/Young Surgeons/data/mod_gensurg_new_cost_and_icu.csv")

trad_ortho_icu <- trad_ortho_icu[,c("newID_1","newID_10","total_cost_complete", "total_cost_index_only","icu_days","total_cost_other", "icu_yes")]
mod_ortho_icu <- mod_ortho_icu[,c("newID_1","newID_10","total_cost_complete", "total_cost_index_only","icu_days","total_cost_other", "icu_yes")]
trad_gensurg_icu <- trad_gensurg_icu[,c("newID_1","newID_10","total_cost_complete", "total_cost_index_only","icu_days","total_cost_other", "icu_yes")]
mod_gensurg_icu <- mod_gensurg_icu[,c("newID_1","newID_10","total_cost_complete", "total_cost_index_only","icu_days","total_cost_other","icu_yes")]
new_icu_data <- rbind(trad_ortho_icu, mod_ortho_icu, trad_gensurg_icu, mod_gensurg_icu)

both_0 <- merge(x=both_0, y=new_icu_data, by=c("newID_1","newID_10"),all.x=T)
both_1 <- merge(x=both_1, y=new_icu_data, by=c("newID_1","newID_10"),all.x=T)
both_2 <- merge(x=both_2, y=new_icu_data, by=c("newID_1","newID_10"),all.x=T)
both_3 <- merge(x=both_3, y=new_icu_data, by=c("newID_1","newID_10"),all.x=T)

####################################################
##### Read in the readms_or_death_new variable. ####
tg_readms <- read.csv("/Users/sharpe/Desktop/Young Surgeons/data/transfer_trad_gensurg_new_readms.csv")
mg_readms <- read.csv("/Users/sharpe/Desktop/Young Surgeons/data/transfer_mod_gensurg_new_readms.csv")
to_readms <- read.csv("/Users/sharpe/Desktop/Young Surgeons/data/transfer_trad_ortho_new_readms.csv")
mo_readms <- read.csv("/Users/sharpe/Desktop/Young Surgeons/data/transfer_mod_ortho_new_readms.csv")
new_readms_data <- rbind.fill(tg_readms, mg_readms, to_readms, mo_readms)

both_0 <- merge(x=both_0, y=new_readms_data, by=c("newID_1","newID_10"),all.x=T)
both_1 <- merge(x=both_1, y=new_readms_data, by=c("newID_1","newID_10"),all.x=T)
both_2 <- merge(x=both_2, y=new_readms_data, by=c("newID_1","newID_10"),all.x=T)
both_3 <- merge(x=both_3, y=new_readms_data, by=c("newID_1","newID_10"),all.x=T)




##############################################
#### Read in and apply all of the scores. ####

###################################################################################
#### Read in the risk and propensity scores from the final level of the taper. ####



risk_ortho_post_2010 <- read.csv("/Users/sharpe/Desktop/Young Surgeons/table/risk_model_mod_ortho_post_2010_11.8.2018_exact_procs.csv")
risk_ortho_pre_2010 <- read.csv("/Users/sharpe/Desktop/Young Surgeons/table/risk_model_mod_ortho_post_2010_11.8.2018_exact_procs.csv")
risk_gensurg_post_2010 <- read.csv("/Users/sharpe/Desktop/Young Surgeons/table/risk_model_mod_gensurg_post_2010_11.2.2018.csv")
risk_gensurg_pre_2010 <- read.csv("/Users/sharpe/Desktop/Young Surgeons/table/risk_model_mod_gensurg_pre_2010_11.2.2018.csv")

crisk_ortho_post_2010 <- read.csv("/Users/sharpe/Desktop/Young Surgeons/table/crisk_model_mod_ortho_post_2010_11.8.2018_exact_procs.csv")
crisk_ortho_pre_2010 <- read.csv("/Users/sharpe/Desktop/Young Surgeons/table/crisk_model_mod_ortho_post_2010_11.8.2018_exact_procs.csv")
crisk_gensurg_post_2010 <- read.csv("/Users/sharpe/Desktop/Young Surgeons/table/crisk_model_mod_gensurg_post_2010_11.2.2018.csv")
crisk_gensurg_pre_2010 <- read.csv("/Users/sharpe/Desktop/Young Surgeons/table/crisk_model_mod_gensurg_pre_2010_11.2.2018.csv")

prop_mod_gensurg_pre_2010_lv_2 <- read.csv("/Users/sharpe/Desktop/Young Surgeons/taper_table/lv_2_propensity_model_mod_gensurg_pre_2010.csv")
prop_mod_gensurg_post_2010_lv_2 <- read.csv("/Users/sharpe/Desktop/Young Surgeons/taper_table/lv_2_propensity_model_mod_gensurg_post_2010.csv")
prop_mod_ortho_pre_2010_lv_2 <- read.csv("/Users/sharpe/Desktop/Young Surgeons/taper_table/lv_2_propensity_model_mod_ortho_pre_2010.csv")
prop_mod_ortho_post_2010_lv_2 <- read.csv("/Users/sharpe/Desktop/Young Surgeons/taper_table/lv_2_propensity_model_mod_ortho_post_2010.csv")

prop_mod_gensurg_pre_2010_lv_1 <- read.csv("/Users/sharpe/Desktop/Young Surgeons/taper_table/lv_1_propensity_model_mod_gensurg_pre_2010.csv")
prop_mod_gensurg_post_2010_lv_1 <- read.csv("/Users/sharpe/Desktop/Young Surgeons/taper_table/lv_1_propensity_model_mod_gensurg_post_2010.csv")
prop_mod_ortho_pre_2010_lv_1 <- read.csv("/Users/sharpe/Desktop/Young Surgeons/taper_table/lv_1_propensity_model_mod_ortho_pre_2010.csv")
prop_mod_ortho_post_2010_lv_1 <- read.csv("/Users/sharpe/Desktop/Young Surgeons/taper_table/lv_1_propensity_model_mod_ortho_post_2010.csv")

prop_mod_gensurg_pre_2010_lv_0 <- read.csv("/Users/sharpe/Desktop/Young Surgeons/taper_table/lv_0_propensity_model_mod_gensurg_pre_2010.csv")
prop_mod_gensurg_post_2010_lv_0 <- read.csv("/Users/sharpe/Desktop/Young Surgeons/taper_table/lv_0_propensity_model_mod_gensurg_post_2010.csv")
prop_mod_ortho_pre_2010_lv_0 <- read.csv("/Users/sharpe/Desktop/Young Surgeons/taper_table/lv_0_propensity_model_mod_ortho_pre_2010.csv")
prop_mod_ortho_post_2010_lv_0 <- read.csv("/Users/sharpe/Desktop/Young Surgeons/taper_table/lv_0_propensity_model_mod_ortho_post_2010.csv")

prop_mod_gensurg_pre_2010_lv_3 <- read.csv("/Users/sharpe/Desktop/Young Surgeons/table/propensity_model_mod_gensurg_pre_2010_11.2.2018.csv")
prop_mod_gensurg_post_2010_lv_3 <- read.csv("/Users/sharpe/Desktop/Young Surgeons/table/propensity_model_mod_gensurg_post_2010_11.2.2018.csv")
prop_mod_ortho_pre_2010_lv_3 <- read.csv("/Users/sharpe/Desktop/Young Surgeons/table/propensity_model_mod_ortho_pre_2010_11.8.2018_exact_procs.csv")
prop_mod_ortho_post_2010_lv_3 <- read.csv("/Users/sharpe/Desktop/Young Surgeons/table/propensity_model_mod_ortho_post_2010_11.8.2018_exact_procs.csv")




apply_model <- function(model,dataset,varname1,varname2,era){
  
  
  #### Subset data to only the variables that are in the model. ####
  get_model_vars <- as.character(model[(2:nrow(model)),1])
  temp_data <- as.matrix(subset(dataset, select=get_model_vars))
  
  #### Save the model intercept and coefficients. ####
  intercept <- model[1,2]
  coefs <- as.matrix(model[(2:nrow(model)),2])
  
  #### Multiply the coefficients by the data. ####
  scores <- as.data.frame(temp_data %*% coefs + intercept)
  dataset <- cbind(as.data.frame(dataset), scores)
  colnames(dataset)[dim(dataset)[2]] <- varname1
  
  #### Get the probability version of the scores. ####
  probs <- exp(scores)/(1+exp(scores))
  dataset <- cbind(dataset, probs)
  colnames(dataset)[dim(dataset)[2]] <- varname2
  
  
  return(dataset)
  
}

both_0 <- apply_model(model=risk_ortho_post_2010, dataset=both_0, varname1="risk_logit_ortho_post_2010", varname2="risk_prob_ortho_post_2010", era="mod")
both_0 <- apply_model(model=risk_ortho_pre_2010, dataset=both_0, varname1="risk_logit_ortho_pre_2010", varname2="risk_prob_ortho_pre_2010", era="mod")
both_0 <- apply_model(model=risk_gensurg_post_2010, dataset=both_0, varname1="risk_logit_gensurg_post_2010", varname2="risk_prob_gensurg_post_2010", era="mod")
both_0 <- apply_model(model=risk_gensurg_pre_2010, dataset=both_0, varname1="risk_logit_gensurg_pre_2010", varname2="risk_prob_gensurg_pre_2010", era="mod")
both_0 <- apply_model(model=crisk_ortho_post_2010, dataset=both_0, varname1="crisk_logit_ortho_post_2010", varname2="crisk_prob_ortho_post_2010", era="mod")
both_0 <- apply_model(model=crisk_ortho_pre_2010, dataset=both_0, varname1="crisk_logit_ortho_pre_2010", varname2="crisk_prob_ortho_pre_2010", era="mod")
both_0 <- apply_model(model=crisk_gensurg_post_2010, dataset=both_0, varname1="crisk_logit_gensurg_post_2010", varname2="crisk_prob_gensurg_post_2010", era="mod")
both_0 <- apply_model(model=crisk_gensurg_pre_2010, dataset=both_0, varname1="crisk_logit_gensurg_pre_2010", varname2="crisk_prob_gensurg_pre_2010", era="mod")
both_0 <- apply_model(model=prop_mod_gensurg_pre_2010_lv_0, dataset=both_0, varname1="prop_lv_0_gensurg_logit_pre_2010", varname2="prop_lv_0_gensurg_prob_pre_2010", era="mod")
both_0 <- apply_model(model=prop_mod_gensurg_post_2010_lv_0, dataset=both_0, varname1="prop_lv_0_gensurg_logit_post_2010", varname2="prop_lv_0_gensurg_prob_post_2010", era="mod")
both_0 <- apply_model(model=prop_mod_ortho_pre_2010_lv_0, dataset=both_0, varname1="prop_lv_0_ortho_logit_pre_2010", varname2="prop_lv_0_ortho_prob_pre_2010", era="mod")
both_0 <- apply_model(model=prop_mod_ortho_post_2010_lv_0, dataset=both_0, varname1="prop_lv_0_ortho_logit_post_2010", varname2="prop_lv_0_ortho_prob_post_2010", era="mod")
both_0 <- apply_model(model=prop_mod_gensurg_pre_2010_lv_1, dataset=both_0, varname1="prop_lv_1_gensurg_logit_pre_2010", varname2="prop_lv_1_gensurg_prob_pre_2010", era="mod")
both_0 <- apply_model(model=prop_mod_gensurg_post_2010_lv_1, dataset=both_0, varname1="prop_lv_1_gensurg_logit_post_2010", varname2="prop_lv_1_gensurg_prob_post_2010", era="mod")
both_0 <- apply_model(model=prop_mod_ortho_pre_2010_lv_1, dataset=both_0, varname1="prop_lv_1_ortho_logit_pre_2010", varname2="prop_lv_1_ortho_prob_pre_2010", era="mod")
both_0 <- apply_model(model=prop_mod_ortho_post_2010_lv_1, dataset=both_0, varname1="prop_lv_1_ortho_logit_post_2010", varname2="prop_lv_1_ortho_prob_post_2010", era="mod")
both_0 <- apply_model(model=prop_mod_gensurg_pre_2010_lv_2, dataset=both_0, varname1="prop_lv_2_gensurg_logit_pre_2010", varname2="prop_lv_2_gensurg_prob_pre_2010", era="mod")
both_0 <- apply_model(model=prop_mod_gensurg_post_2010_lv_2, dataset=both_0, varname1="prop_lv_2_gensurg_logit_post_2010", varname2="prop_lv_2_gensurg_prob_post_2010", era="mod")
both_0 <- apply_model(model=prop_mod_ortho_pre_2010_lv_2, dataset=both_0, varname1="prop_lv_2_ortho_logit_pre_2010", varname2="prop_lv_2_ortho_prob_pre_2010", era="mod")
both_0 <- apply_model(model=prop_mod_ortho_post_2010_lv_2, dataset=both_0, varname1="prop_lv_2_ortho_logit_post_2010", varname2="prop_lv_2_ortho_prob_post_2010", era="mod")
both_0 <- apply_model(model=prop_mod_gensurg_pre_2010_lv_3, dataset=both_0, varname1="prop_lv_3_gensurg_logit_pre_2010", varname2="prop_lv_3_gensurg_prob_pre_2010", era="mod")
both_0 <- apply_model(model=prop_mod_gensurg_post_2010_lv_3, dataset=both_0, varname1="prop_lv_3_gensurg_logit_post_2010", varname2="prop_lv_3_gensurg_prob_post_2010", era="mod")
both_0 <- apply_model(model=prop_mod_ortho_pre_2010_lv_3, dataset=both_0, varname1="prop_lv_3_ortho_logit_pre_2010", varname2="prop_lv_3_ortho_prob_pre_2010", era="mod")
both_0 <- apply_model(model=prop_mod_ortho_post_2010_lv_3, dataset=both_0, varname1="prop_lv_3_ortho_logit_post_2010", varname2="prop_lv_3_ortho_prob_post_2010", era="mod")


both_1 <- apply_model(model=risk_ortho_post_2010, dataset=both_1, varname1="risk_logit_ortho_post_2010", varname2="risk_prob_ortho_post_2010", era="mod")
both_1 <- apply_model(model=risk_ortho_pre_2010, dataset=both_1, varname1="risk_logit_ortho_pre_2010", varname2="risk_prob_ortho_pre_2010", era="mod")
both_1 <- apply_model(model=risk_gensurg_post_2010, dataset=both_1, varname1="risk_logit_gensurg_post_2010", varname2="risk_prob_gensurg_post_2010", era="mod")
both_1 <- apply_model(model=risk_gensurg_pre_2010, dataset=both_1, varname1="risk_logit_gensurg_pre_2010", varname2="risk_prob_gensurg_pre_2010", era="mod")
both_1 <- apply_model(model=crisk_ortho_post_2010, dataset=both_1, varname1="crisk_logit_ortho_post_2010", varname2="crisk_prob_ortho_post_2010", era="mod")
both_1 <- apply_model(model=crisk_ortho_pre_2010, dataset=both_1, varname1="crisk_logit_ortho_pre_2010", varname2="crisk_prob_ortho_pre_2010", era="mod")
both_1 <- apply_model(model=crisk_gensurg_post_2010, dataset=both_1, varname1="crisk_logit_gensurg_post_2010", varname2="crisk_prob_gensurg_post_2010", era="mod")
both_1 <- apply_model(model=crisk_gensurg_pre_2010, dataset=both_1, varname1="crisk_logit_gensurg_pre_2010", varname2="crisk_prob_gensurg_pre_2010", era="mod")
both_1 <- apply_model(model=prop_mod_gensurg_pre_2010_lv_0, dataset=both_1, varname1="prop_lv_0_gensurg_logit_pre_2010", varname2="prop_lv_0_gensurg_prob_pre_2010", era="mod")
both_1 <- apply_model(model=prop_mod_gensurg_post_2010_lv_0, dataset=both_1, varname1="prop_lv_0_gensurg_logit_post_2010", varname2="prop_lv_0_gensurg_prob_post_2010", era="mod")
both_1 <- apply_model(model=prop_mod_ortho_pre_2010_lv_0, dataset=both_1, varname1="prop_lv_0_ortho_logit_pre_2010", varname2="prop_lv_0_ortho_prob_pre_2010", era="mod")
both_1 <- apply_model(model=prop_mod_ortho_post_2010_lv_0, dataset=both_1, varname1="prop_lv_0_ortho_logit_post_2010", varname2="prop_lv_0_ortho_prob_post_2010", era="mod")
both_1 <- apply_model(model=prop_mod_gensurg_pre_2010_lv_1, dataset=both_1, varname1="prop_lv_1_gensurg_logit_pre_2010", varname2="prop_lv_1_gensurg_prob_pre_2010", era="mod")
both_1 <- apply_model(model=prop_mod_gensurg_post_2010_lv_1, dataset=both_1, varname1="prop_lv_1_gensurg_logit_post_2010", varname2="prop_lv_1_gensurg_prob_post_2010", era="mod")
both_1 <- apply_model(model=prop_mod_ortho_pre_2010_lv_1, dataset=both_1, varname1="prop_lv_1_ortho_logit_pre_2010", varname2="prop_lv_1_ortho_prob_pre_2010", era="mod")
both_1 <- apply_model(model=prop_mod_ortho_post_2010_lv_1, dataset=both_1, varname1="prop_lv_1_ortho_logit_post_2010", varname2="prop_lv_1_ortho_prob_post_2010", era="mod")
both_1 <- apply_model(model=prop_mod_gensurg_pre_2010_lv_2, dataset=both_1, varname1="prop_lv_2_gensurg_logit_pre_2010", varname2="prop_lv_2_gensurg_prob_pre_2010", era="mod")
both_1 <- apply_model(model=prop_mod_gensurg_post_2010_lv_2, dataset=both_1, varname1="prop_lv_2_gensurg_logit_post_2010", varname2="prop_lv_2_gensurg_prob_post_2010", era="mod")
both_1 <- apply_model(model=prop_mod_ortho_pre_2010_lv_2, dataset=both_1, varname1="prop_lv_2_ortho_logit_pre_2010", varname2="prop_lv_2_ortho_prob_pre_2010", era="mod")
both_1 <- apply_model(model=prop_mod_ortho_post_2010_lv_2, dataset=both_1, varname1="prop_lv_2_ortho_logit_post_2010", varname2="prop_lv_2_ortho_prob_post_2010", era="mod")
both_1 <- apply_model(model=prop_mod_gensurg_pre_2010_lv_3, dataset=both_1, varname1="prop_lv_3_gensurg_logit_pre_2010", varname2="prop_lv_3_gensurg_prob_pre_2010", era="mod")
both_1 <- apply_model(model=prop_mod_gensurg_post_2010_lv_3, dataset=both_1, varname1="prop_lv_3_gensurg_logit_post_2010", varname2="prop_lv_3_gensurg_prob_post_2010", era="mod")
both_1 <- apply_model(model=prop_mod_ortho_pre_2010_lv_3, dataset=both_1, varname1="prop_lv_3_ortho_logit_pre_2010", varname2="prop_lv_3_ortho_prob_pre_2010", era="mod")
both_1 <- apply_model(model=prop_mod_ortho_post_2010_lv_3, dataset=both_1, varname1="prop_lv_3_ortho_logit_post_2010", varname2="prop_lv_3_ortho_prob_post_2010", era="mod")

both_2 <- apply_model(model=risk_ortho_post_2010, dataset=both_2, varname1="risk_logit_ortho_post_2010", varname2="risk_prob_ortho_post_2010", era="mod")
both_2 <- apply_model(model=risk_ortho_pre_2010, dataset=both_2, varname1="risk_logit_ortho_pre_2010", varname2="risk_prob_ortho_pre_2010", era="mod")
both_2 <- apply_model(model=risk_gensurg_post_2010, dataset=both_2, varname1="risk_logit_gensurg_post_2010", varname2="risk_prob_gensurg_post_2010", era="mod")
both_2 <- apply_model(model=risk_gensurg_pre_2010, dataset=both_2, varname1="risk_logit_gensurg_pre_2010", varname2="risk_prob_gensurg_pre_2010", era="mod")
both_2 <- apply_model(model=crisk_ortho_post_2010, dataset=both_2, varname1="crisk_logit_ortho_post_2010", varname2="crisk_prob_ortho_post_2010", era="mod")
both_2 <- apply_model(model=crisk_ortho_pre_2010, dataset=both_2, varname1="crisk_logit_ortho_pre_2010", varname2="crisk_prob_ortho_pre_2010", era="mod")
both_2 <- apply_model(model=crisk_gensurg_post_2010, dataset=both_2, varname1="crisk_logit_gensurg_post_2010", varname2="crisk_prob_gensurg_post_2010", era="mod")
both_2 <- apply_model(model=crisk_gensurg_pre_2010, dataset=both_2, varname1="crisk_logit_gensurg_pre_2010", varname2="crisk_prob_gensurg_pre_2010", era="mod")
both_2 <- apply_model(model=prop_mod_gensurg_pre_2010_lv_0, dataset=both_2, varname1="prop_lv_0_gensurg_logit_pre_2010", varname2="prop_lv_0_gensurg_prob_pre_2010", era="mod")
both_2 <- apply_model(model=prop_mod_gensurg_post_2010_lv_0, dataset=both_2, varname1="prop_lv_0_gensurg_logit_post_2010", varname2="prop_lv_0_gensurg_prob_post_2010", era="mod")
both_2 <- apply_model(model=prop_mod_ortho_pre_2010_lv_0, dataset=both_2, varname1="prop_lv_0_ortho_logit_pre_2010", varname2="prop_lv_0_ortho_prob_pre_2010", era="mod")
both_2 <- apply_model(model=prop_mod_ortho_post_2010_lv_0, dataset=both_2, varname1="prop_lv_0_ortho_logit_post_2010", varname2="prop_lv_0_ortho_prob_post_2010", era="mod")
both_2 <- apply_model(model=prop_mod_gensurg_pre_2010_lv_1, dataset=both_2, varname1="prop_lv_1_gensurg_logit_pre_2010", varname2="prop_lv_1_gensurg_prob_pre_2010", era="mod")
both_2 <- apply_model(model=prop_mod_gensurg_post_2010_lv_1, dataset=both_2, varname1="prop_lv_1_gensurg_logit_post_2010", varname2="prop_lv_1_gensurg_prob_post_2010", era="mod")
both_2 <- apply_model(model=prop_mod_ortho_pre_2010_lv_1, dataset=both_2, varname1="prop_lv_1_ortho_logit_pre_2010", varname2="prop_lv_1_ortho_prob_pre_2010", era="mod")
both_2 <- apply_model(model=prop_mod_ortho_post_2010_lv_1, dataset=both_2, varname1="prop_lv_1_ortho_logit_post_2010", varname2="prop_lv_1_ortho_prob_post_2010", era="mod")
both_2 <- apply_model(model=prop_mod_gensurg_pre_2010_lv_2, dataset=both_2, varname1="prop_lv_2_gensurg_logit_pre_2010", varname2="prop_lv_2_gensurg_prob_pre_2010", era="mod")
both_2 <- apply_model(model=prop_mod_gensurg_post_2010_lv_2, dataset=both_2, varname1="prop_lv_2_gensurg_logit_post_2010", varname2="prop_lv_2_gensurg_prob_post_2010", era="mod")
both_2 <- apply_model(model=prop_mod_ortho_pre_2010_lv_2, dataset=both_2, varname1="prop_lv_2_ortho_logit_pre_2010", varname2="prop_lv_2_ortho_prob_pre_2010", era="mod")
both_2 <- apply_model(model=prop_mod_ortho_post_2010_lv_2, dataset=both_2, varname1="prop_lv_2_ortho_logit_post_2010", varname2="prop_lv_2_ortho_prob_post_2010", era="mod")
both_2 <- apply_model(model=prop_mod_gensurg_pre_2010_lv_3, dataset=both_2, varname1="prop_lv_3_gensurg_logit_pre_2010", varname2="prop_lv_3_gensurg_prob_pre_2010", era="mod")
both_2 <- apply_model(model=prop_mod_gensurg_post_2010_lv_3, dataset=both_2, varname1="prop_lv_3_gensurg_logit_post_2010", varname2="prop_lv_3_gensurg_prob_post_2010", era="mod")
both_2 <- apply_model(model=prop_mod_ortho_pre_2010_lv_3, dataset=both_2, varname1="prop_lv_3_ortho_logit_pre_2010", varname2="prop_lv_3_ortho_prob_pre_2010", era="mod")
both_2 <- apply_model(model=prop_mod_ortho_post_2010_lv_3, dataset=both_2, varname1="prop_lv_3_ortho_logit_post_2010", varname2="prop_lv_3_ortho_prob_post_2010", era="mod")

both_3 <- apply_model(model=risk_ortho_post_2010, dataset=both_3, varname1="risk_logit_ortho_post_2010", varname2="risk_prob_ortho_post_2010", era="mod")
both_3 <- apply_model(model=risk_ortho_pre_2010, dataset=both_3, varname1="risk_logit_ortho_pre_2010", varname2="risk_prob_ortho_pre_2010", era="mod")
both_3 <- apply_model(model=risk_gensurg_post_2010, dataset=both_3, varname1="risk_logit_gensurg_post_2010", varname2="risk_prob_gensurg_post_2010", era="mod")
both_3 <- apply_model(model=risk_gensurg_pre_2010, dataset=both_3, varname1="risk_logit_gensurg_pre_2010", varname2="risk_prob_gensurg_pre_2010", era="mod")
both_3 <- apply_model(model=crisk_ortho_post_2010, dataset=both_3, varname1="crisk_logit_ortho_post_2010", varname2="crisk_prob_ortho_post_2010", era="mod")
both_3 <- apply_model(model=crisk_ortho_pre_2010, dataset=both_3, varname1="crisk_logit_ortho_pre_2010", varname2="crisk_prob_ortho_pre_2010", era="mod")
both_3 <- apply_model(model=crisk_gensurg_post_2010, dataset=both_3, varname1="crisk_logit_gensurg_post_2010", varname2="crisk_prob_gensurg_post_2010", era="mod")
both_3 <- apply_model(model=crisk_gensurg_pre_2010, dataset=both_3, varname1="crisk_logit_gensurg_pre_2010", varname2="crisk_prob_gensurg_pre_2010", era="mod")
both_3 <- apply_model(model=prop_mod_gensurg_pre_2010_lv_0, dataset=both_3, varname1="prop_lv_0_gensurg_logit_pre_2010", varname2="prop_lv_0_gensurg_prob_pre_2010", era="mod")
both_3 <- apply_model(model=prop_mod_gensurg_post_2010_lv_0, dataset=both_3, varname1="prop_lv_0_gensurg_logit_post_2010", varname2="prop_lv_0_gensurg_prob_post_2010", era="mod")
both_3 <- apply_model(model=prop_mod_ortho_pre_2010_lv_0, dataset=both_3, varname1="prop_lv_0_ortho_logit_pre_2010", varname2="prop_lv_0_ortho_prob_pre_2010", era="mod")
both_3 <- apply_model(model=prop_mod_ortho_post_2010_lv_0, dataset=both_3, varname1="prop_lv_0_ortho_logit_post_2010", varname2="prop_lv_0_ortho_prob_post_2010", era="mod")
both_3 <- apply_model(model=prop_mod_gensurg_pre_2010_lv_1, dataset=both_3, varname1="prop_lv_1_gensurg_logit_pre_2010", varname2="prop_lv_1_gensurg_prob_pre_2010", era="mod")
both_3 <- apply_model(model=prop_mod_gensurg_post_2010_lv_1, dataset=both_3, varname1="prop_lv_1_gensurg_logit_post_2010", varname2="prop_lv_1_gensurg_prob_post_2010", era="mod")
both_3 <- apply_model(model=prop_mod_ortho_pre_2010_lv_1, dataset=both_3, varname1="prop_lv_1_ortho_logit_pre_2010", varname2="prop_lv_1_ortho_prob_pre_2010", era="mod")
both_3 <- apply_model(model=prop_mod_ortho_post_2010_lv_1, dataset=both_3, varname1="prop_lv_1_ortho_logit_post_2010", varname2="prop_lv_1_ortho_prob_post_2010", era="mod")
both_3 <- apply_model(model=prop_mod_gensurg_pre_2010_lv_2, dataset=both_3, varname1="prop_lv_2_gensurg_logit_pre_2010", varname2="prop_lv_2_gensurg_prob_pre_2010", era="mod")
both_3 <- apply_model(model=prop_mod_gensurg_post_2010_lv_2, dataset=both_3, varname1="prop_lv_2_gensurg_logit_post_2010", varname2="prop_lv_2_gensurg_prob_post_2010", era="mod")
both_3 <- apply_model(model=prop_mod_ortho_pre_2010_lv_2, dataset=both_3, varname1="prop_lv_2_ortho_logit_pre_2010", varname2="prop_lv_2_ortho_prob_pre_2010", era="mod")
both_3 <- apply_model(model=prop_mod_ortho_post_2010_lv_2, dataset=both_3, varname1="prop_lv_2_ortho_logit_post_2010", varname2="prop_lv_2_ortho_prob_post_2010", era="mod")
both_3 <- apply_model(model=prop_mod_gensurg_pre_2010_lv_3, dataset=both_3, varname1="prop_lv_3_gensurg_logit_pre_2010", varname2="prop_lv_3_gensurg_prob_pre_2010", era="mod")
both_3 <- apply_model(model=prop_mod_gensurg_post_2010_lv_3, dataset=both_3, varname1="prop_lv_3_gensurg_logit_post_2010", varname2="prop_lv_3_gensurg_prob_post_2010", era="mod")
both_3 <- apply_model(model=prop_mod_ortho_pre_2010_lv_3, dataset=both_3, varname1="prop_lv_3_ortho_logit_pre_2010", varname2="prop_lv_3_ortho_prob_pre_2010", era="mod")
both_3 <- apply_model(model=prop_mod_ortho_post_2010_lv_3, dataset=both_3, varname1="prop_lv_3_ortho_logit_post_2010", varname2="prop_lv_3_ortho_prob_post_2010", era="mod")



both_0$risk_logit_mod <- ifelse(both_0$oct_2010_plus==1 & both_0$gensurg_adm_mod==1, both_0$risk_logit_gensurg_post_2010,
                                ifelse(both_0$oct_2010_plus==0 & both_0$gensurg_adm_mod==1, both_0$risk_logit_gensurg_pre_2010,
                                       ifelse(both_0$oct_2010_plus==1 & both_0$ortho_adm_mod==1, both_0$risk_logit_ortho_post_2010,
                                              both_0$risk_logit_ortho_pre_2010)))
both_0$risk_prob_mod <- ifelse(both_0$oct_2010_plus==1 & both_0$gensurg_adm_mod==1, both_0$risk_prob_gensurg_post_2010,
                               ifelse(both_0$oct_2010_plus==0 & both_0$gensurg_adm_mod==1, both_0$risk_prob_gensurg_pre_2010,
                                      ifelse(both_0$oct_2010_plus==1 & both_0$ortho_adm_mod==1, both_0$risk_prob_ortho_post_2010,
                                             both_0$risk_prob_ortho_pre_2010)))
both_0$crisk_logit_mod <- ifelse(both_0$oct_2010_plus==1 & both_0$gensurg_adm_mod==1, both_0$crisk_logit_gensurg_post_2010,
                                 ifelse(both_0$oct_2010_plus==0 & both_0$gensurg_adm_mod==1, both_0$crisk_logit_gensurg_pre_2010,
                                        ifelse(both_0$oct_2010_plus==1 & both_0$ortho_adm_mod==1, both_0$crisk_logit_ortho_post_2010,
                                               both_0$crisk_logit_ortho_pre_2010)))
both_0$crisk_prob_mod <- ifelse(both_0$oct_2010_plus==1 & both_0$gensurg_adm_mod==1, both_0$crisk_prob_gensurg_post_2010,
                                ifelse(both_0$oct_2010_plus==0 & both_0$gensurg_adm_mod==1, both_0$crisk_prob_gensurg_pre_2010,
                                       ifelse(both_0$oct_2010_plus==1 & both_0$ortho_adm_mod==1, both_0$crisk_prob_ortho_post_2010,
                                              both_0$crisk_prob_ortho_pre_2010)))
both_0$prop_lv_0_logit <- ifelse(both_0$oct_2010_plus==1 & both_0$gensurg_adm_mod==1, both_0$prop_lv_0_gensurg_logit_post_2010,
                                 ifelse(both_0$oct_2010_plus==0 & both_0$gensurg_adm_mod==1, both_0$prop_lv_0_gensurg_logit_pre_2010,
                                        ifelse(both_0$oct_2010_plus==1 & both_0$ortho_adm_mod==1, both_0$prop_lv_0_ortho_logit_post_2010,
                                               both_0$prop_lv_0_ortho_logit_pre_2010)))
both_0$prop_lv_0_prob <- ifelse(both_0$oct_2010_plus==1 & both_0$gensurg_adm_mod==1, both_0$prop_lv_0_gensurg_prob_post_2010,
                                ifelse(both_0$oct_2010_plus==0 & both_0$gensurg_adm_mod==1, both_0$prop_lv_0_gensurg_prob_pre_2010,
                                       ifelse(both_0$oct_2010_plus==1 & both_0$ortho_adm_mod==1, both_0$prop_lv_0_ortho_prob_post_2010,
                                              both_0$prop_lv_0_ortho_prob_pre_2010)))
both_0$prop_lv_1_logit <- ifelse(both_0$oct_2010_plus==1 & both_0$gensurg_adm_mod==1, both_0$prop_lv_1_gensurg_logit_post_2010,
                                 ifelse(both_0$oct_2010_plus==0 & both_0$gensurg_adm_mod==1, both_0$prop_lv_1_gensurg_logit_pre_2010,
                                        ifelse(both_0$oct_2010_plus==1 & both_0$ortho_adm_mod==1, both_0$prop_lv_1_ortho_logit_post_2010,
                                               both_0$prop_lv_1_ortho_logit_pre_2010)))
both_0$prop_lv_1_prob <- ifelse(both_0$oct_2010_plus==1 & both_0$gensurg_adm_mod==1, both_0$prop_lv_1_gensurg_prob_post_2010,
                                ifelse(both_0$oct_2010_plus==0 & both_0$gensurg_adm_mod==1, both_0$prop_lv_1_gensurg_prob_pre_2010,
                                       ifelse(both_0$oct_2010_plus==1 & both_0$ortho_adm_mod==1, both_0$prop_lv_1_ortho_prob_post_2010,
                                              both_0$prop_lv_1_ortho_prob_pre_2010)))
both_0$prop_lv_2_logit <- ifelse(both_0$oct_2010_plus==1 & both_0$gensurg_adm_mod==1, both_0$prop_lv_2_gensurg_logit_post_2010,
                                 ifelse(both_0$oct_2010_plus==0 & both_0$gensurg_adm_mod==1, both_0$prop_lv_2_gensurg_logit_pre_2010,
                                        ifelse(both_0$oct_2010_plus==1 & both_0$ortho_adm_mod==1, both_0$prop_lv_2_ortho_logit_post_2010,
                                               both_0$prop_lv_2_ortho_logit_pre_2010)))
both_0$prop_lv_2_prob <- ifelse(both_0$oct_2010_plus==1 & both_0$gensurg_adm_mod==1, both_0$prop_lv_2_gensurg_prob_post_2010,
                                ifelse(both_0$oct_2010_plus==0 & both_0$gensurg_adm_mod==1, both_0$prop_lv_2_gensurg_prob_pre_2010,
                                       ifelse(both_0$oct_2010_plus==1 & both_0$ortho_adm_mod==1, both_0$prop_lv_2_ortho_prob_post_2010,
                                              both_0$prop_lv_2_ortho_prob_pre_2010)))
both_0$prop_lv_3_logit <- ifelse(both_0$oct_2010_plus==1 & both_0$gensurg_adm_mod==1, both_0$prop_lv_3_gensurg_logit_post_2010,
                                 ifelse(both_0$oct_2010_plus==0 & both_0$gensurg_adm_mod==1, both_0$prop_lv_3_gensurg_logit_pre_2010,
                                        ifelse(both_0$oct_2010_plus==1 & both_0$ortho_adm_mod==1, both_0$prop_lv_3_ortho_logit_post_2010,
                                               both_0$prop_lv_3_ortho_logit_pre_2010)))
both_0$prop_lv_3_prob <- ifelse(both_0$oct_2010_plus==1 & both_0$gensurg_adm_mod==1, both_0$prop_lv_3_gensurg_prob_post_2010,
                                ifelse(both_0$oct_2010_plus==0 & both_0$gensurg_adm_mod==1, both_0$prop_lv_3_gensurg_prob_pre_2010,
                                       ifelse(both_0$oct_2010_plus==1 & both_0$ortho_adm_mod==1, both_0$prop_lv_3_ortho_prob_post_2010,
                                              both_0$prop_lv_3_ortho_prob_pre_2010)))

both_1$risk_logit_mod <- ifelse(both_1$oct_2010_plus==1 & both_1$gensurg_adm_mod==1, both_1$risk_logit_gensurg_post_2010,
                                ifelse(both_1$oct_2010_plus==0 & both_1$gensurg_adm_mod==1, both_1$risk_logit_gensurg_pre_2010,
                                       ifelse(both_1$oct_2010_plus==1 & both_1$ortho_adm_mod==1, both_1$risk_logit_ortho_post_2010,
                                              both_1$risk_logit_ortho_pre_2010)))
both_1$risk_prob_mod <- ifelse(both_1$oct_2010_plus==1 & both_1$gensurg_adm_mod==1, both_1$risk_prob_gensurg_post_2010,
                               ifelse(both_1$oct_2010_plus==0 & both_1$gensurg_adm_mod==1, both_1$risk_prob_gensurg_pre_2010,
                                      ifelse(both_1$oct_2010_plus==1 & both_1$ortho_adm_mod==1, both_1$risk_prob_ortho_post_2010,
                                             both_1$risk_prob_ortho_pre_2010)))
both_1$crisk_logit_mod <- ifelse(both_1$oct_2010_plus==1 & both_1$gensurg_adm_mod==1, both_1$crisk_logit_gensurg_post_2010,
                                 ifelse(both_1$oct_2010_plus==0 & both_1$gensurg_adm_mod==1, both_1$crisk_logit_gensurg_pre_2010,
                                        ifelse(both_1$oct_2010_plus==1 & both_1$ortho_adm_mod==1, both_1$crisk_logit_ortho_post_2010,
                                               both_1$crisk_logit_ortho_pre_2010)))
both_1$crisk_prob_mod <- ifelse(both_1$oct_2010_plus==1 & both_1$gensurg_adm_mod==1, both_1$crisk_prob_gensurg_post_2010,
                                ifelse(both_1$oct_2010_plus==0 & both_1$gensurg_adm_mod==1, both_1$crisk_prob_gensurg_pre_2010,
                                       ifelse(both_1$oct_2010_plus==1 & both_1$ortho_adm_mod==1, both_1$crisk_prob_ortho_post_2010,
                                              both_1$crisk_prob_ortho_pre_2010)))
both_1$prop_lv_0_logit <- ifelse(both_1$oct_2010_plus==1 & both_1$gensurg_adm_mod==1, both_1$prop_lv_0_gensurg_logit_post_2010,
                                 ifelse(both_1$oct_2010_plus==0 & both_1$gensurg_adm_mod==1, both_1$prop_lv_0_gensurg_logit_pre_2010,
                                        ifelse(both_1$oct_2010_plus==1 & both_1$ortho_adm_mod==1, both_1$prop_lv_0_ortho_logit_post_2010,
                                               both_1$prop_lv_0_ortho_logit_pre_2010)))
both_1$prop_lv_0_prob <- ifelse(both_1$oct_2010_plus==1 & both_1$gensurg_adm_mod==1, both_1$prop_lv_0_gensurg_prob_post_2010,
                                ifelse(both_1$oct_2010_plus==0 & both_1$gensurg_adm_mod==1, both_1$prop_lv_0_gensurg_prob_pre_2010,
                                       ifelse(both_1$oct_2010_plus==1 & both_1$ortho_adm_mod==1, both_1$prop_lv_0_ortho_prob_post_2010,
                                              both_1$prop_lv_0_ortho_prob_pre_2010)))
both_1$prop_lv_1_logit <- ifelse(both_1$oct_2010_plus==1 & both_1$gensurg_adm_mod==1, both_1$prop_lv_1_gensurg_logit_post_2010,
                                 ifelse(both_1$oct_2010_plus==0 & both_1$gensurg_adm_mod==1, both_1$prop_lv_1_gensurg_logit_pre_2010,
                                        ifelse(both_1$oct_2010_plus==1 & both_1$ortho_adm_mod==1, both_1$prop_lv_1_ortho_logit_post_2010,
                                               both_1$prop_lv_1_ortho_logit_pre_2010)))
both_1$prop_lv_1_prob <- ifelse(both_1$oct_2010_plus==1 & both_1$gensurg_adm_mod==1, both_1$prop_lv_1_gensurg_prob_post_2010,
                                ifelse(both_1$oct_2010_plus==0 & both_1$gensurg_adm_mod==1, both_1$prop_lv_1_gensurg_prob_pre_2010,
                                       ifelse(both_1$oct_2010_plus==1 & both_1$ortho_adm_mod==1, both_1$prop_lv_1_ortho_prob_post_2010,
                                              both_1$prop_lv_1_ortho_prob_pre_2010)))
both_1$prop_lv_2_logit <- ifelse(both_1$oct_2010_plus==1 & both_1$gensurg_adm_mod==1, both_1$prop_lv_2_gensurg_logit_post_2010,
                                 ifelse(both_1$oct_2010_plus==0 & both_1$gensurg_adm_mod==1, both_1$prop_lv_2_gensurg_logit_pre_2010,
                                        ifelse(both_1$oct_2010_plus==1 & both_1$ortho_adm_mod==1, both_1$prop_lv_2_ortho_logit_post_2010,
                                               both_1$prop_lv_2_ortho_logit_pre_2010)))
both_1$prop_lv_2_prob <- ifelse(both_1$oct_2010_plus==1 & both_1$gensurg_adm_mod==1, both_1$prop_lv_2_gensurg_prob_post_2010,
                                ifelse(both_1$oct_2010_plus==0 & both_1$gensurg_adm_mod==1, both_1$prop_lv_2_gensurg_prob_pre_2010,
                                       ifelse(both_1$oct_2010_plus==1 & both_1$ortho_adm_mod==1, both_1$prop_lv_2_ortho_prob_post_2010,
                                              both_1$prop_lv_2_ortho_prob_pre_2010)))
both_1$prop_lv_3_logit <- ifelse(both_1$oct_2010_plus==1 & both_1$gensurg_adm_mod==1, both_1$prop_lv_3_gensurg_logit_post_2010,
                                 ifelse(both_1$oct_2010_plus==0 & both_1$gensurg_adm_mod==1, both_1$prop_lv_3_gensurg_logit_pre_2010,
                                        ifelse(both_1$oct_2010_plus==1 & both_1$ortho_adm_mod==1, both_1$prop_lv_3_ortho_logit_post_2010,
                                               both_1$prop_lv_3_ortho_logit_pre_2010)))
both_1$prop_lv_3_prob <- ifelse(both_1$oct_2010_plus==1 & both_1$gensurg_adm_mod==1, both_1$prop_lv_3_gensurg_prob_post_2010,
                                ifelse(both_1$oct_2010_plus==0 & both_1$gensurg_adm_mod==1, both_1$prop_lv_3_gensurg_prob_pre_2010,
                                       ifelse(both_1$oct_2010_plus==1 & both_1$ortho_adm_mod==1, both_1$prop_lv_3_ortho_prob_post_2010,
                                              both_1$prop_lv_3_ortho_prob_pre_2010)))

both_2$risk_logit_mod <- ifelse(both_2$oct_2010_plus==1 & both_2$gensurg_adm_mod==1, both_2$risk_logit_gensurg_post_2010,
                                ifelse(both_2$oct_2010_plus==0 & both_2$gensurg_adm_mod==1, both_2$risk_logit_gensurg_pre_2010,
                                       ifelse(both_2$oct_2010_plus==1 & both_2$ortho_adm_mod==1, both_2$risk_logit_ortho_post_2010,
                                              both_2$risk_logit_ortho_pre_2010)))
both_2$risk_prob_mod <- ifelse(both_2$oct_2010_plus==1 & both_2$gensurg_adm_mod==1, both_2$risk_prob_gensurg_post_2010,
                               ifelse(both_2$oct_2010_plus==0 & both_2$gensurg_adm_mod==1, both_2$risk_prob_gensurg_pre_2010,
                                      ifelse(both_2$oct_2010_plus==1 & both_2$ortho_adm_mod==1, both_2$risk_prob_ortho_post_2010,
                                             both_2$risk_prob_ortho_pre_2010)))
both_2$crisk_logit_mod <- ifelse(both_2$oct_2010_plus==1 & both_2$gensurg_adm_mod==1, both_2$crisk_logit_gensurg_post_2010,
                                 ifelse(both_2$oct_2010_plus==0 & both_2$gensurg_adm_mod==1, both_2$crisk_logit_gensurg_pre_2010,
                                        ifelse(both_2$oct_2010_plus==1 & both_2$ortho_adm_mod==1, both_2$crisk_logit_ortho_post_2010,
                                               both_2$crisk_logit_ortho_pre_2010)))
both_2$crisk_prob_mod <- ifelse(both_2$oct_2010_plus==1 & both_2$gensurg_adm_mod==1, both_2$crisk_prob_gensurg_post_2010,
                                ifelse(both_2$oct_2010_plus==0 & both_2$gensurg_adm_mod==1, both_2$crisk_prob_gensurg_pre_2010,
                                       ifelse(both_2$oct_2010_plus==1 & both_2$ortho_adm_mod==1, both_2$crisk_prob_ortho_post_2010,
                                              both_2$crisk_prob_ortho_pre_2010)))
both_2$prop_lv_0_logit <- ifelse(both_2$oct_2010_plus==1 & both_2$gensurg_adm_mod==1, both_2$prop_lv_0_gensurg_logit_post_2010,
                                 ifelse(both_2$oct_2010_plus==0 & both_2$gensurg_adm_mod==1, both_2$prop_lv_0_gensurg_logit_pre_2010,
                                        ifelse(both_2$oct_2010_plus==1 & both_2$ortho_adm_mod==1, both_2$prop_lv_0_ortho_logit_post_2010,
                                               both_2$prop_lv_0_ortho_logit_pre_2010)))
both_2$prop_lv_0_prob <- ifelse(both_2$oct_2010_plus==1 & both_2$gensurg_adm_mod==1, both_2$prop_lv_0_gensurg_prob_post_2010,
                                ifelse(both_2$oct_2010_plus==0 & both_2$gensurg_adm_mod==1, both_2$prop_lv_0_gensurg_prob_pre_2010,
                                       ifelse(both_2$oct_2010_plus==1 & both_2$ortho_adm_mod==1, both_2$prop_lv_0_ortho_prob_post_2010,
                                              both_2$prop_lv_0_ortho_prob_pre_2010)))
both_2$prop_lv_1_logit <- ifelse(both_2$oct_2010_plus==1 & both_2$gensurg_adm_mod==1, both_2$prop_lv_1_gensurg_logit_post_2010,
                                 ifelse(both_2$oct_2010_plus==0 & both_2$gensurg_adm_mod==1, both_2$prop_lv_1_gensurg_logit_pre_2010,
                                        ifelse(both_2$oct_2010_plus==1 & both_2$ortho_adm_mod==1, both_2$prop_lv_1_ortho_logit_post_2010,
                                               both_2$prop_lv_1_ortho_logit_pre_2010)))
both_2$prop_lv_1_prob <- ifelse(both_2$oct_2010_plus==1 & both_2$gensurg_adm_mod==1, both_2$prop_lv_1_gensurg_prob_post_2010,
                                ifelse(both_2$oct_2010_plus==0 & both_2$gensurg_adm_mod==1, both_2$prop_lv_1_gensurg_prob_pre_2010,
                                       ifelse(both_2$oct_2010_plus==1 & both_2$ortho_adm_mod==1, both_2$prop_lv_1_ortho_prob_post_2010,
                                              both_2$prop_lv_1_ortho_prob_pre_2010)))
both_2$prop_lv_2_logit <- ifelse(both_2$oct_2010_plus==1 & both_2$gensurg_adm_mod==1, both_2$prop_lv_2_gensurg_logit_post_2010,
                                 ifelse(both_2$oct_2010_plus==0 & both_2$gensurg_adm_mod==1, both_2$prop_lv_2_gensurg_logit_pre_2010,
                                        ifelse(both_2$oct_2010_plus==1 & both_2$ortho_adm_mod==1, both_2$prop_lv_2_ortho_logit_post_2010,
                                               both_2$prop_lv_2_ortho_logit_pre_2010)))
both_2$prop_lv_2_prob <- ifelse(both_2$oct_2010_plus==1 & both_2$gensurg_adm_mod==1, both_2$prop_lv_2_gensurg_prob_post_2010,
                                ifelse(both_2$oct_2010_plus==0 & both_2$gensurg_adm_mod==1, both_2$prop_lv_2_gensurg_prob_pre_2010,
                                       ifelse(both_2$oct_2010_plus==1 & both_2$ortho_adm_mod==1, both_2$prop_lv_2_ortho_prob_post_2010,
                                              both_2$prop_lv_2_ortho_prob_pre_2010)))
both_2$prop_lv_3_logit <- ifelse(both_2$oct_2010_plus==1 & both_2$gensurg_adm_mod==1, both_2$prop_lv_3_gensurg_logit_post_2010,
                                 ifelse(both_2$oct_2010_plus==0 & both_2$gensurg_adm_mod==1, both_2$prop_lv_3_gensurg_logit_pre_2010,
                                        ifelse(both_2$oct_2010_plus==1 & both_2$ortho_adm_mod==1, both_2$prop_lv_3_ortho_logit_post_2010,
                                               both_2$prop_lv_3_ortho_logit_pre_2010)))
both_2$prop_lv_3_prob <- ifelse(both_2$oct_2010_plus==1 & both_2$gensurg_adm_mod==1, both_2$prop_lv_3_gensurg_prob_post_2010,
                                ifelse(both_2$oct_2010_plus==0 & both_2$gensurg_adm_mod==1, both_2$prop_lv_3_gensurg_prob_pre_2010,
                                       ifelse(both_2$oct_2010_plus==1 & both_2$ortho_adm_mod==1, both_2$prop_lv_3_ortho_prob_post_2010,
                                              both_2$prop_lv_3_ortho_prob_pre_2010)))

both_3$risk_logit_mod <- ifelse(both_3$oct_2010_plus==1 & both_3$gensurg_adm_mod==1, both_3$risk_logit_gensurg_post_2010,
                                ifelse(both_3$oct_2010_plus==0 & both_3$gensurg_adm_mod==1, both_3$risk_logit_gensurg_pre_2010,
                                       ifelse(both_3$oct_2010_plus==1 & both_3$ortho_adm_mod==1, both_3$risk_logit_ortho_post_2010,
                                              both_3$risk_logit_ortho_pre_2010)))
both_3$risk_prob_mod <- ifelse(both_3$oct_2010_plus==1 & both_3$gensurg_adm_mod==1, both_3$risk_prob_gensurg_post_2010,
                               ifelse(both_3$oct_2010_plus==0 & both_3$gensurg_adm_mod==1, both_3$risk_prob_gensurg_pre_2010,
                                      ifelse(both_3$oct_2010_plus==1 & both_3$ortho_adm_mod==1, both_3$risk_prob_ortho_post_2010,
                                             both_3$risk_prob_ortho_pre_2010)))
both_3$crisk_logit_mod <- ifelse(both_3$oct_2010_plus==1 & both_3$gensurg_adm_mod==1, both_3$crisk_logit_gensurg_post_2010,
                                 ifelse(both_3$oct_2010_plus==0 & both_3$gensurg_adm_mod==1, both_3$crisk_logit_gensurg_pre_2010,
                                        ifelse(both_3$oct_2010_plus==1 & both_3$ortho_adm_mod==1, both_3$crisk_logit_ortho_post_2010,
                                               both_3$crisk_logit_ortho_pre_2010)))
both_3$crisk_prob_mod <- ifelse(both_3$oct_2010_plus==1 & both_3$gensurg_adm_mod==1, both_3$crisk_prob_gensurg_post_2010,
                                ifelse(both_3$oct_2010_plus==0 & both_3$gensurg_adm_mod==1, both_3$crisk_prob_gensurg_pre_2010,
                                       ifelse(both_3$oct_2010_plus==1 & both_3$ortho_adm_mod==1, both_3$crisk_prob_ortho_post_2010,
                                              both_3$crisk_prob_ortho_pre_2010)))
both_3$prop_lv_0_logit <- ifelse(both_3$oct_2010_plus==1 & both_3$gensurg_adm_mod==1, both_3$prop_lv_0_gensurg_logit_post_2010,
                                 ifelse(both_3$oct_2010_plus==0 & both_3$gensurg_adm_mod==1, both_3$prop_lv_0_gensurg_logit_pre_2010,
                                        ifelse(both_3$oct_2010_plus==1 & both_3$ortho_adm_mod==1, both_3$prop_lv_0_ortho_logit_post_2010,
                                               both_3$prop_lv_0_ortho_logit_pre_2010)))
both_3$prop_lv_0_prob <- ifelse(both_3$oct_2010_plus==1 & both_3$gensurg_adm_mod==1, both_3$prop_lv_0_gensurg_prob_post_2010,
                                ifelse(both_3$oct_2010_plus==0 & both_3$gensurg_adm_mod==1, both_3$prop_lv_0_gensurg_prob_pre_2010,
                                       ifelse(both_3$oct_2010_plus==1 & both_3$ortho_adm_mod==1, both_3$prop_lv_0_ortho_prob_post_2010,
                                              both_3$prop_lv_0_ortho_prob_pre_2010)))
both_3$prop_lv_1_logit <- ifelse(both_3$oct_2010_plus==1 & both_3$gensurg_adm_mod==1, both_3$prop_lv_1_gensurg_logit_post_2010,
                                 ifelse(both_3$oct_2010_plus==0 & both_3$gensurg_adm_mod==1, both_3$prop_lv_1_gensurg_logit_pre_2010,
                                        ifelse(both_3$oct_2010_plus==1 & both_3$ortho_adm_mod==1, both_3$prop_lv_1_ortho_logit_post_2010,
                                               both_3$prop_lv_1_ortho_logit_pre_2010)))
both_3$prop_lv_1_prob <- ifelse(both_3$oct_2010_plus==1 & both_3$gensurg_adm_mod==1, both_3$prop_lv_1_gensurg_prob_post_2010,
                                ifelse(both_3$oct_2010_plus==0 & both_3$gensurg_adm_mod==1, both_3$prop_lv_1_gensurg_prob_pre_2010,
                                       ifelse(both_3$oct_2010_plus==1 & both_3$ortho_adm_mod==1, both_3$prop_lv_1_ortho_prob_post_2010,
                                              both_3$prop_lv_1_ortho_prob_pre_2010)))
both_3$prop_lv_2_logit <- ifelse(both_3$oct_2010_plus==1 & both_3$gensurg_adm_mod==1, both_3$prop_lv_2_gensurg_logit_post_2010,
                                 ifelse(both_3$oct_2010_plus==0 & both_3$gensurg_adm_mod==1, both_3$prop_lv_2_gensurg_logit_pre_2010,
                                        ifelse(both_3$oct_2010_plus==1 & both_3$ortho_adm_mod==1, both_3$prop_lv_2_ortho_logit_post_2010,
                                               both_3$prop_lv_2_ortho_logit_pre_2010)))
both_3$prop_lv_2_prob <- ifelse(both_3$oct_2010_plus==1 & both_3$gensurg_adm_mod==1, both_3$prop_lv_2_gensurg_prob_post_2010,
                                ifelse(both_3$oct_2010_plus==0 & both_3$gensurg_adm_mod==1, both_3$prop_lv_2_gensurg_prob_pre_2010,
                                       ifelse(both_3$oct_2010_plus==1 & both_3$ortho_adm_mod==1, both_3$prop_lv_2_ortho_prob_post_2010,
                                              both_3$prop_lv_2_ortho_prob_pre_2010)))
both_3$prop_lv_3_logit <- ifelse(both_3$oct_2010_plus==1 & both_3$gensurg_adm_mod==1, both_3$prop_lv_3_gensurg_logit_post_2010,
                                 ifelse(both_3$oct_2010_plus==0 & both_3$gensurg_adm_mod==1, both_3$prop_lv_3_gensurg_logit_pre_2010,
                                        ifelse(both_3$oct_2010_plus==1 & both_3$ortho_adm_mod==1, both_3$prop_lv_3_ortho_logit_post_2010,
                                               both_3$prop_lv_3_ortho_logit_pre_2010)))
both_3$prop_lv_3_prob <- ifelse(both_3$oct_2010_plus==1 & both_3$gensurg_adm_mod==1, both_3$prop_lv_3_gensurg_prob_post_2010,
                                ifelse(both_3$oct_2010_plus==0 & both_3$gensurg_adm_mod==1, both_3$prop_lv_3_gensurg_prob_pre_2010,
                                       ifelse(both_3$oct_2010_plus==1 & both_3$ortho_adm_mod==1, both_3$prop_lv_3_ortho_prob_post_2010,
                                              both_3$prop_lv_3_ortho_prob_pre_2010)))



###########################
#### Define variables. ####

comorbidities <- c("silb_CHF","silb_PastMi","silb_PastARR","silb_ANGINA","silb_Valvulardis","silb_UnstAngina","silb_HTN","silb_DEMENT","silb_LIVER","silb_PARAPLEG","silb_RENLDYS","silb_renlfail",
                   "silb_SEIZUR","silb_STROKE","silb_AbdCancer","silb_ASTHMA","silb_CANCER","silb_CHRNLUNG","silb_DIABETES","silb_PostPulmFibr",
                   "silb_AIDS","silb_ChrPepticUlcer","silb_COAG","silb_CollagenVascular","silb_CongenitalCoag","silb_CUSHINGS","silb_GRAVES","silb_HYPOTHY","silb_SickleCell","silb_THROMB")

npgs <- c("hip_repair","hip_replacement","hip_replacement_revision","knee_replacement","knee_replacement_revision",
          "cholecystectomy","colectomy_partial__open","parathyroidectomy" ,"proctectomy","hernia_abdominal_open",
          "mastectomy","small_bowel_resection","hernia_groin_open","lysis_of_adhesions","stomach_other",
          "appendectomy","stomach_partial_gastrectomy","stomach_gastric_bypass_nonbariatric", "ulcer","small_bowel_other",
          "stomach_antireflux","splenectomy","pancreatectomy","pd_access_procedure","colectomy_total__open",
          "biliary_common_duct","biliary_other","stomach_total_gastrectomy","large_bowel_other","thyroidectomy_partial",
          "liver_partial_hepatectomy","thyroidectomy_total", "proctopexy" ,"liver_other","pyloroplasty",
          "bariatric","adrenalectomy","esophagectomy","esophagomyotomy","thyroidectomy_substernal",
          "colectomy_partial__lap", "hernia_abdominal_lap","hernia_groin_lap", "colectomy_total__lap" )

score_vars <- c("prop_lv_0_logit","prop_lv_0_prob", "prop_lv_1_logit","prop_lv_1_prob","prop_lv_2_logit","prop_lv_2_prob","prop_lv_3_logit","prop_lv_3_prob",
                "risk_logit_mod", "risk_prob_mod", "crisk_logit_mod", "crisk_prob_mod")

other_vars <- c("age_65_69", "age_70_74", "age_75_79", "age_80_85", "age_85_plus", "age_at_admsn", "admsn_in_quarters", "comorb_sum",
                "sex_female","oct_2010_plus", "jan_2012_plus", "emergent_type", "transfer_24hrs_ip","past_adm_6mo", "admit_year")

bin_vars <- c(comorbidities, npgs, "age_65_69", "age_70_74", "age_75_79", "age_80_85", "age_85_plus",
              "sex_female","oct_2010_plus", "jan_2012_plus", "emergent_type", "transfer_24hrs_ip","past_adm_6mo")
con_vars <- c("age_at_admsn", "admsn_in_quarters", "comorb_sum", score_vars, "admit_year")
bin_outcomes <- c("readms_30","readms_or_death","dead_inhosp","ftr_30_ign_poa","ftr_30_use_poa","ftr_inhosp_ign_poa","ftr_inhosp_use_poa","icu_yes",
                  grep(x=names(both_0),pattern="complic_inhosp_",value=TRUE), "dead_30","dead_60","dead_90","dead_180","prolonged_d","step_up","complic_poa_any",
                  "readms30_post", "readms_or_death_new")
bin_outcomes <- bin_outcomes[-which(bin_outcomes=="complic_inhosp_COMPSYN")]

con_outcomes <- c("total_cost_complete","total_cost_index_only","anesth_time_claim","icu_days","los_long", "basic_los")
all_vars <- c(con_vars, bin_vars, bin_outcomes, con_outcomes)

################################
#### Do the exterior match. ####

ext_match_0_1 <- ext_match_paired(  data1=both_0,
                             data2=both_1,
                             treatment_var="new_surgeon_flag",
                             pat_id="newID_1",
                             pair_id="pair_id_pats",
                             final_table_labels=c("New Surgeon Patients","Exp Surgeon Patients - Lv 0","Exp Surgeon Patients - Lv 1","Exp Surgeon Pats - in 0 but not 1","Exp Surgeon Pats - in 1 but not 0", "P-Value"),
                             bin_vars=c(bin_vars, bin_outcomes),
                             con_vars=c(con_vars, con_outcomes),
                             bin_outcomes=bin_outcomes,
                             con_outcomes=con_outcomes,
                             vars_ordered=c("admit_year", "admsn_in_quarters", bin_vars, con_vars[which(!(con_vars %in% c("admit_year", "admsn_in_quarters")))], bin_outcomes, con_outcomes))

ext_match_1_2 <- ext_match_paired(  data1=both_1,
                             data2=both_2,
                             treatment_var="new_surgeon_flag",
                             pat_id="newID_1",
                             pair_id="pair_id_pats",
                             final_table_labels=c("New Surgeon Patients","Exp Surgeon Patients - Lv 1","Exp Surgeon Patients - Lv 2","Exp Surgeon Pats - in 1 but not 2","Exp Surgeon Pats - in 2 but not 1", "P-Value"),
                             bin_vars=c(bin_vars, bin_outcomes),
                             con_vars=c(con_vars, con_outcomes),
                             bin_outcomes=bin_outcomes,
                             con_outcomes=con_outcomes,
                             vars_ordered=c("admit_year", "admsn_in_quarters", npgs, bin_vars[which(!(bin_vars %in% npgs))], con_vars[which(!(con_vars %in% c("admit_year", "admsn_in_quarters")))], bin_outcomes, con_outcomes))


ext_match_2_3 <- ext_match_paired(  data1=both_2,
                             data2=both_3,
                             treatment_var="new_surgeon_flag",
                             pat_id="newID_1",
                             pair_id="pair_id_pats",
                             final_table_labels=c("New Surgeon Patients","Exp Surgeon Patients - Lv 2","Exp Surgeon Patients - Lv 3","Exp Surgeon Pats - in 2 but not 3","Exp Surgeon Pats - in 3 but not 2", "P-Value"),
                             bin_vars=c(bin_vars, bin_outcomes),
                             con_vars=c(con_vars, con_outcomes),
                             bin_outcomes=bin_outcomes,
                             con_outcomes=con_outcomes,
                             vars_ordered=c("admit_year", "admsn_in_quarters", npgs, "emergent_type", bin_vars[which(!(bin_vars %in% c(npgs, "emergent_type")))], con_vars[which(!(con_vars %in% c("admit_year", "admsn_in_quarters")))], bin_outcomes, con_outcomes))




ext_match_0_1_gs <- ext_match_paired(  data1=both_0[which(both_0$gensurg_adm_mod==1),],
                                data2=both_1[which(both_1$gensurg_adm_mod==1),],
                                treatment_var="new_surgeon_flag",
                                pat_id="newID_1",
                                pair_id="pair_id_pats",
                                final_table_labels=c("New Surgeon Patients","Exp Surgeon Patients - Lv 0","Exp Surgeon Patients - Lv 1","Exp Surgeon Pats - in 0 but not 1","Exp Surgeon Pats - in 1 but not 0", "P-Value"),
                                bin_vars=c(bin_vars, bin_outcomes),
                                con_vars=c(con_vars, con_outcomes),
                                bin_outcomes=bin_outcomes,
                                con_outcomes=con_outcomes,
                                vars_ordered=c("admit_year", "admsn_in_quarters", bin_vars, con_vars[which(!(con_vars %in% c("admit_year", "admsn_in_quarters")))], bin_outcomes, con_outcomes))

ext_match_1_2_gs <- ext_match_paired(  data1=both_1[which(both_1$gensurg_adm_mod==1),],
                                data2=both_2[which(both_2$gensurg_adm_mod==1), ],
                                treatment_var="new_surgeon_flag",
                                pat_id="newID_1",
                                pair_id="pair_id_pats",
                                final_table_labels=c("New Surgeon Patients","Exp Surgeon Patients - Lv 1","Exp Surgeon Patients - Lv 2","Exp Surgeon Pats - in 1 but not 2","Exp Surgeon Pats - in 2 but not 1", "P-Value"),
                                bin_vars=c(bin_vars, bin_outcomes),
                                con_vars=c(con_vars, con_outcomes),
                                bin_outcomes=bin_outcomes,
                                con_outcomes=con_outcomes,
                                vars_ordered=c("admit_year", "admsn_in_quarters", npgs, bin_vars[which(!(bin_vars %in% npgs))], con_vars[which(!(con_vars %in% c("admit_year", "admsn_in_quarters")))], bin_outcomes, con_outcomes))


ext_match_2_3_gs <- ext_match_paired(  data1=both_2[which(both_2$gensurg_adm_mod==1),],
                                data2=both_3[which(both_3$gensurg_adm_mod==1),],
                                treatment_var="new_surgeon_flag",
                                pat_id="newID_1",
                                pair_id="pair_id_pats",
                                final_table_labels=c("New Surgeon Patients","Exp Surgeon Patients - Lv 2","Exp Surgeon Patients - Lv 3","Exp Surgeon Pats - in 2 but not 3","Exp Surgeon Pats - in 3 but not 2", "P-Value"),
                                bin_vars=c(bin_vars, bin_outcomes),
                                con_vars=c(con_vars, con_outcomes),
                                bin_outcomes=bin_outcomes,
                                con_outcomes=con_outcomes,
                                vars_ordered=c("admit_year", "admsn_in_quarters", npgs, "emergent_type", bin_vars[which(!(bin_vars %in% c(npgs, "emergent_type")))], con_vars[which(!(con_vars %in% c("admit_year", "admsn_in_quarters")))], bin_outcomes, con_outcomes))


ext_match_0_1_o <- ext_match_paired(  data1=both_0[which(both_0$ortho_adm_mod==1),],
                               data2=both_1[which(both_1$ortho_adm_mod==1),],
                               treatment_var="new_surgeon_flag",
                               pat_id="newID_1",
                               pair_id="pair_id_pats",
                               final_table_labels=c("New Surgeon Patients","Exp Surgeon Patients - Lv 0","Exp Surgeon Patients - Lv 1","Exp Surgeon Pats - in 0 but not 1","Exp Surgeon Pats - in 1 but not 0", "P-Value"),
                               bin_vars=c(bin_vars, bin_outcomes),
                               con_vars=c(con_vars, con_outcomes),
                               bin_outcomes=bin_outcomes,
                               con_outcomes=con_outcomes,
                               vars_ordered=c("admit_year", "admsn_in_quarters", bin_vars, con_vars[which(!(con_vars %in% c("admit_year", "admsn_in_quarters")))], bin_outcomes, con_outcomes))

ext_match_1_2_o <- ext_match_paired(  data1=both_1[which(both_1$ortho_adm_mod==1),],
                               data2=both_2[which(both_2$ortho_adm_mod==1), ],
                               treatment_var="new_surgeon_flag",
                               pat_id="newID_1",
                               pair_id="pair_id_pats",
                               final_table_labels=c("New Surgeon Patients","Exp Surgeon Patients - Lv 1","Exp Surgeon Patients - Lv 2","Exp Surgeon Pats - in 1 but not 2","Exp Surgeon Pats - in 2 but not 1", "P-Value"),
                               bin_vars=c(bin_vars, bin_outcomes),
                               con_vars=c(con_vars, con_outcomes),
                               bin_outcomes=bin_outcomes,
                               con_outcomes=con_outcomes,
                               vars_ordered=c("admit_year", "admsn_in_quarters", npgs, bin_vars[which(!(bin_vars %in% npgs))], con_vars[which(!(con_vars %in% c("admit_year", "admsn_in_quarters")))], bin_outcomes, con_outcomes))


ext_match_2_3_o <- ext_match_paired(  data1=both_2[which(both_2$ortho_adm_mod==1),],
                               data2=both_3[which(both_3$ortho_adm_mod==1),],
                               treatment_var="new_surgeon_flag",
                               pat_id="newID_1",
                               pair_id="pair_id_pats",
                               final_table_labels=c("New Surgeon Patients","Exp Surgeon Patients - Lv 2","Exp Surgeon Patients - Lv 3","Exp Surgeon Pats - in 2 but not 3","Exp Surgeon Pats - in 3 but not 2", "P-Value"),
                               bin_vars=c(bin_vars, bin_outcomes),
                               con_vars=c(con_vars, con_outcomes),
                               bin_outcomes=bin_outcomes,
                               con_outcomes=con_outcomes,
                               vars_ordered=c("admit_year", "admsn_in_quarters", npgs, "emergent_type", bin_vars[which(!(bin_vars %in% c(npgs, "emergent_type")))], con_vars[which(!(con_vars %in% c("admit_year", "admsn_in_quarters")))], bin_outcomes, con_outcomes))



write.csv(x=ext_match_0_1, file="/Users/sharpe/Desktop/Young Surgeons/taper_table/ext_match_lvs_0_1_paired.csv")
write.csv(x=ext_match_1_2, file="/Users/sharpe/Desktop/Young Surgeons/taper_table/ext_match_lvs_1_2_paired.csv")
write.csv(x=ext_match_2_3, file="/Users/sharpe/Desktop/Young Surgeons/taper_table/ext_match_lvs_2_3_paired.csv")
write.csv(x=ext_match_0_1_gs, file="/Users/sharpe/Desktop/Young Surgeons/taper_table/ext_match_lvs_0_1_gensurg_paired.csv")
write.csv(x=ext_match_1_2_gs, file="/Users/sharpe/Desktop/Young Surgeons/taper_table/ext_match_lvs_1_2_gensurg_paired.csv")
write.csv(x=ext_match_2_3_gs, file="/Users/sharpe/Desktop/Young Surgeons/taper_table/ext_match_lvs_2_3_gensurg_paired.csv")
write.csv(x=ext_match_0_1_o, file="/Users/sharpe/Desktop/Young Surgeons/taper_table/ext_match_lvs_0_1_ortho_paired.csv")
write.csv(x=ext_match_1_2_o, file="/Users/sharpe/Desktop/Young Surgeons/taper_table/ext_match_lvs_1_2_ortho_paired.csv")
write.csv(x=ext_match_2_3_o, file="/Users/sharpe/Desktop/Young Surgeons/taper_table/ext_match_lvs_2_3_ortho_paired.csv")




